package controllers;

import java.util.Date;
import java.util.List;

import models.CommonTable;
import models.TableRelation;
import models.User;
import play.data.validation.Required;
import utils.Paged;

public class TableRelationController extends BaseController{
    
    
    private static String craeteQueryHql(Long maintainerId) {
        StringBuilder hql = new StringBuilder(" 1 = 1 ");
        if (maintainerId != null) {
            hql.append(" and maintainer.id = '").append(maintainerId).append("'");
        }
        return hql.toString();
    }
    
    public static void list(Long maintainerId, int currentPage) {
        String hql = craeteQueryHql( maintainerId);
        currentPage = getCurrentPage(currentPage);
        int count = new Long(TableRelation.count(hql)).intValue();
        List<TableRelation> tables = TableRelation.find(hql).from((currentPage - 1) * PAGE_SIZE).fetch(PAGE_SIZE);
        Paged<TableRelation> paged = new Paged<TableRelation>(tables, currentPage, PAGE_SIZE, count);
        List<User> maintainers = User.findActiveUsers();
        render("/tablerelation/list.html", paged, maintainers, maintainerId);
    }
    
    
    public static void save(@Required(message = "relation.srcTable.require") Long srcTableId,@Required(message = "relation.targetTable.require") Long targetTableId,@Required(message = "relation.maintainer.require") Long maintainerId, String remark) {
        System.out.println(srcTableId );
        if (validation.hasErrors()) {
            params.flash();
            validation.keep();
            form(null);
        }
        
        TableRelation relation = new TableRelation();
        relation = toSetTableRelationValue(relation, srcTableId, targetTableId, maintainerId, remark);
        User current = getCurrentUser();
        relation.author = current;
        relation.createDate = new Date();
        relation.save();
        form(relation.id);
    }
    
    public static void update(Long relationId,@Required(message = "relation.srcTable.require") Long srcTableId,@Required(message = "relation.targetTable.require") Long targetTableId,@Required(message = "relation.maintainer.require") Long maintainerId, String remark) {
        if (validation.hasErrors()) {
            params.flash();
            validation.keep();
            form(relationId);
        }
        TableRelation relation = TableRelation.findById(relationId);
        if (!isExistTable(relation)) {
            notFound();
        }
        toSetTableRelationValue(relation, srcTableId, targetTableId, maintainerId, remark).save();
        form(relationId);
    }
    
    private static TableRelation toSetTableRelationValue(TableRelation relation, Long srcTableId, Long targetTableId, Long maintainerId, String remark) {
        CommonTable srcTable = new CommonTable(srcTableId);
        CommonTable targetTable = new CommonTable(targetTableId);
        User maintainer = new User(maintainerId);
        
        User current = getCurrentUser();
        
        relation.srcTable = srcTable;
        relation.targetTable = targetTable;
        relation.maintainer = maintainer;
        relation.updateDate = new Date();
        relation.updater = current;
        relation.remark = remark;
        return relation;
    }
    
    private static boolean isExistTable(TableRelation table) {
        return null != table;
    }

    public static void form(Long relationId) {
        List<User> maintainers = User.findActiveUsers();
        List<CommonTable> srcTables =  CommonTable.findActiveTable();
        List<CommonTable> targetTables = CommonTable.findActiveTable();
        
        if (relationId == null) {
            render("/tablerelation/form.html", maintainers, srcTables, targetTables);
        }
        
        TableRelation relation = TableRelation.findById(relationId);
        if (relation == null) {
            notFound();
        }
        
        render("/tablerelation/form.html", relation, maintainers, srcTables, targetTables);
    }
    
    public static void delete (Long relationId) {
        TableRelation.delete("id", relationId);
        list(null, FIRST_PAGE);
    }

}
